<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\ApprovalList;
use App\Models\ApprovalTemplate;
use App\Models\User;
use App\Models\ApprovalLog;
use Illuminate\Support\Facades\Auth;
use Tymon\JWTAuth\Facades\JWTAuth;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\Hash;
use Maatwebsite\Excel\Concerns\ToArray;

// 审批列表
class ApprovalListController extends Controller
{
    // 列表
    public function index(Request $request)
    {

        $is_all = $request->input('is_all', 0); //不分页获取全部数据

        $apply_status = $request->input('apply_status', ''); // 审批状态
        $template_id = $request->input('template_id', ''); // 审批类型
        $grid_name = $request->input('grid_name', ''); // 网格
        $grid_group_id = $request->input('grid_group_id', ''); // 网格组
        $street_id = $request->input('street_id', ''); // 街道
        $village_id = $request->input('village_id', ''); // 村庄
        $user_id = $request->input('user_id', ''); // 申请人

        $select_time = $request->input('select_time', ''); // 筛选时间

        $status = ['申请中', '同意', '拒绝','审批中'];
        if ($is_all) {
            $data = ApprovalList::query()
                ->with('user:username,id')
                ->with('approval_template:name,id')
                ->with('grid:name,id')
                ->where($this->getAppWhere())
                ->when($apply_status, function ($query) use ($apply_status) {
                    $query->where('apply_status', '=', $apply_status);
                })
                ->when($template_id, function ($query) use ($template_id) {
                    $query->where('apply_type', '=', $template_id);
                })
                ->when($grid_name, function ($query) use ($grid_name) {
                    $query->whereHas('grid', function($query) use ($grid_name){
                        $query->where('name', $grid_name);
                    });
                })
                ->when($grid_group_id, function ($query) use ($grid_group_id) {
                    $query->whereHas('grid', function($query) use ($grid_group_id){
                        $query->where('grid_group_id', $grid_group_id);
                    });
                })
                ->when($street_id, function ($query) use ($street_id) {
                    $query->whereHas('grid', function($query) use ($street_id){
                        $query->where('street_id', $street_id);
                    });
                })
                ->when($village_id, function ($query) use ($village_id) {
                    $query->whereHas('grid', function($query) use ($village_id){
                        $query->where('village_id', $village_id);
                    });
                })
                ->when($user_id, function ($query) use ($user_id) {
                    $query->where('user_id', '=', $user_id);
                })
                ->when($select_time, function ($query) use ($select_time) {
                    $select_time=explode(',',$select_time);
                    $query->where('created_at', '>=', $select_time[0])->where('created_at', '<=', $select_time[1]);
                })
                ->orderBy('id')
                ->get()
                ->toArray();
            foreach ($data as &$v) {
                $v['apply_status_str'] = $status[$v['apply_status']];
            }
        } else {
            $data = ApprovalList::query()
                ->with('user:username,id')
                ->with('approval_template:name,id')
                ->with('grid:name,id')
                ->where($this->getAppWhere())
                ->when($apply_status, function ($query) use ($apply_status) {
                    $query->where('apply_status', '=', $apply_status);
                })
                ->when($template_id, function ($query) use ($template_id) {
                    $query->where('apply_type', '=', $template_id);
                })
                ->when($grid_name, function ($query) use ($grid_name) {
                    $query->whereHas('grid', function($query) use ($grid_name){
                        $query->where('name', $grid_name);
                    });
                })
                ->when($grid_group_id, function ($query) use ($grid_group_id) {
                    $query->whereHas('grid', function($query) use ($grid_group_id){
                        $query->where('grid_group_id', $grid_group_id);
                    });
                })
                ->when($street_id, function ($query) use ($street_id) {
                    $query->whereHas('grid', function($query) use ($street_id){
                        $query->where('street_id', $street_id);
                    });
                })
                ->when($village_id, function ($query) use ($village_id) {
                    $query->whereHas('grid', function($query) use ($village_id){
                        $query->where('village_id', $village_id);
                    });
                })
                ->when($user_id, function ($query) use ($user_id) {
                    $query->where('user_id', '=', $user_id);
                })
                ->when($select_time, function ($query) use ($select_time) {
                    $select_time=explode(',',$select_time);
                    $query->where('created_at', '>=', $select_time[0])->where('created_at', '<=', $select_time[1]);
                })
                ->orderByDesc('id')
                ->paginate(env('PAGE_SIZE', 10));
            foreach ($data->items() as &$v) {
                $v['apply_status_str'] = $status[$v['apply_status']];
            }
        }

        return showData($data);
    }

    // 详情
    public function show($id)
    {
        if (empty($id)) {
            return showError('参数错误');
        }

        $li = ApprovalLog::query()
            ->with('user:username,id')
            ->where('list_id', $id)
            ->get()
            ->toArray();
            
        $data = [];
        foreach ($li as $v) {
            $data[$v['step']][] = $v;
        }

        if (empty($data)) {
            return showError('获取信息失败');
        }

        return showData($data);
    }
}
